import { Journal, PageData, getJournalDto, JournalInterfaceWithDerive, postJournalDto, JournalGateway } from "qqlx-core";

import { JournalSchema } from "@/pond/schema-journal";
import { ViewBase, ViewBaseAction } from "@view/view-base";

export class ViewJournal extends ViewBase<Journal> implements ViewBaseAction<Journal>, JournalInterfaceWithDerive<{}> {
  private static instance: ViewJournal;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewJournal();
    }

    if (!this.instance) this.instance = new ViewJournal();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  getSchema(): Journal {
    return new JournalSchema();
  }

  async get() {
    this.pending();
    const dto = { page: this.data.entityListPage, conditionList: this.data.conditionList };
    const res = await this.$request.getByPut<getJournalDto, PageData<Journal>>(JournalGateway.path, dto);
    this.data.entityListTotal = res.total;
    return res;
  }

  async post() {
    this.pending();
    const dto = { schema: this.data.schema };
    return this.$request.post<postJournalDto, null>(JournalGateway.path, dto);
  }
}
